const BaseController = require('./baseController')

/**
 * @class HomerController
 * @extends {BaseController}
 */
class HomerController extends BaseController {

  // 首页
  async index() {
    const { ctx } = this
    const bannerData = await ctx.service.webPageService.getBannerList()

    // 获取技术支持4条数据
    const technologyData = await ctx.service.webPageService.getTechnologyList({})
    // 获取公共数据
    const _commonData = await this.getCommonData()

    await ctx.render('index.nj',{
      ..._commonData,
      technologyData,
      bannerData
    })
  }

  //测试
  // async test() {
  //   const  { ctx } = this
  //   ctx.body = {
  //     data: 1,
  //     code: 200
  //   }
  // }

  // 产品列表页面
  async productList() {
    const { ctx,service } = this
    const params = ctx.request.query
    // 获取公共数据
    const _commonData = await this.getCommonData()
    let curMenu = {}
    // 获取当前菜单信息
    if(params.id) {
      curMenu = await service.webPageService.getCurrentMenu(params)
    }
    // 获取产品列表
    const pData = await service.webPageService.getProductList(params)
  
    await ctx.render('product-list.nj',{
      ..._commonData,
      curMenu,
      ...pData
    })
  }

  // 产品详情页
  async productDetail() {
    const { ctx,service } = this
    const _id = ctx.params.id
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const data = await service.webPageService.getProductDetail(_id)
   
    await ctx.render('product-detail.nj',{
      ..._commonData,
      ...data
    })
  }

  // 技术支持页面
  async technicalSupport() {
    const { ctx,service } = this
    const params = ctx.request.query
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const data = await service.webPageService.getTechnologyList(params)
    await ctx.render('technical-support.nj',{
      ..._commonData,
      data
    })
  }

  //进入留言页面
  async message() {
    const { ctx } = this
    // 获取公共数据
    const _commonData = await this.getCommonData()

    await ctx.render('client-message.nj',{
      ..._commonData,
    })
  }

  //用户留言
  async leaveMessage() {
    const { ctx,service } = this
    const body = ctx.request.body
    ctx.validate({
      content: 'string'
    },body)

    const data = await service.webPageService.inserUserMessage(body)
    ctx.body = {
      code: 200,
      data
    }
  }

  // 技术支持详情页
  async infoDetail() {
    const { ctx,service } = this
    const params = ctx.request.query
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const data = await service.webPageService.getInfoDetail(params)
    
    await ctx.render('info-detail.nj',{
      ..._commonData,
      data
    })
  }

  // 全球战略页面
  async globalStrategy() {
    const { ctx, service } = this
    const params = ctx.request.query
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const data = await service.webPageService.getGlobalCompany(params)
    await ctx.render('global-strategy.nj',{
      ..._commonData,
      data
    })
  }

  // 下载页面
  async fileList() {
    const { ctx,service } = this
    const params = ctx.request.query
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const data = await service.webPageService.getFileList(params)
    const videoData = await service.webPageService.getVideoList(params)
    
    await ctx.render('download.nj',{
      ..._commonData,
      ...data,
      ...videoData
    })
  }

  // 公司简介
  async companProfile() {
    const { ctx,service } = this
    const params = ctx.request.query
    // 获取公共数据
    const _commonData = await this.getCommonData()
    // 获取大事记
    const chronicleData = await service.webPageService.getChronicleList(params)
    const firstVideo = await ctx.model.Video.find({type: 1}).limit(1).populate('posterUrl','link').populate('videoUrl','link')

    await ctx.render('company-profile.nj',{
      ..._commonData,
      chronicleData,
      firstVideo
    })
  }

  // 获取年份查找大事记
  async getChronicleByYear() {
    const { ctx,service } = this
    const params = ctx.request.query.year
    const data = await service.webPageService.getChronicleByYear(params)
    ctx.body = {
      code: 200,
      data
    }
  }

  // 企业文化页面
  async enterpriseCulture() {
    const { ctx,service } = this
    // 获取公共数据
    const _commonData = await this.getCommonData()
    // 获取企业理念
    const philosophyData = await service.webPageService.getPhilosophy()    
    const feelingsData = await service.webPageService.getFeelings()
    const honorData = await service.webPageService.getHonorData()

    await ctx.render('enterprise-culture.nj',{
      ..._commonData,
      philosophyData,
      feelingsData,
      honorData
    })
  }

  // 根据年份朝招荣誉
  async getHonorByYear() {
    const { ctx,service } = this
    const params = ctx.request.query.year
    const data = await service.webPageService.getHonorByYear(params)
    ctx.body = {
      code: 200,
      data
    }
  }

  // 联系我们
  async contactUs() {
    const { ctx,service } = this
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const contactData = await service.webPageService.getContactList()
    await ctx.render('contact-us.nj',{
      ..._commonData,
      contactData
    })
  }

  // 视频详情
  async videoDetail() {
    const { ctx,service } = this
    const _id = ctx.request.query.id
    // 获取公共数据
    const _commonData = await this.getCommonData()
    const data = await service.webPageService.getVideoDetail(_id)
    await ctx.render('video-detail.nj',{
      ..._commonData,
      data
    })
  }

  async getChildCompanyList() {
    const { ctx,service } = this
    const data = await service.webPageService.getAllPointCompany()
    ctx.body = {
      code: 200,
      data
    }
  }

  // 销售商
  async sales() {
    const { ctx } = this
    // 获取公共数据
    const _commonData = await this.getCommonData()
    await ctx.render('sales.nj',{
      ..._commonData
    })
  }

  // 获取所有省份接口
  async getProvinceList() {
    const { ctx } = this
    const data = await ctx.model.Province.find({parentid: 1})
    ctx.body = {
      code: 200,
      data
    }
  }

  //获取指定省份城市
  async getCityList() {
    const { ctx } = this
    const id = ctx.request.query.id
    const data = await ctx.model.Province.find({parentid: id }) 
    ctx.body = {
      code: 200,
      data
    }
  }

  // 获取销售商接口
  async getSalesList() {
    const { ctx,service } = this
    const query = ctx.request.query
    const data = await service.webPageService.getSalesList(query)
    ctx.body = {
      code: 200,
      data
    }
  }
}

module.exports = HomerController